<template>
  <div class="login-container">
    <el-form autoComplete="on" :model="registerForm" :rules="registerRules" ref="registerForm" label-position="left"
             label-width="0px"
             class="card-box login-form">
      <h3 class="ww-logo"></h3>
      <el-form-item prop="name" class="ww-border">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user"/>
        </span>
        <el-input name="name" type="text" v-model="registerForm.name" autoComplete="on" placeholder="请输入姓名"/>
      </el-form-item>
      <el-form-item prop="username" class="ww-border">
        <span class="svg-container svg-container_login">
          <!--<svg-icon icon-class="el-icon-mobile-phone"/>-->
          <i class="el-icon-mobile-phone"></i>
        </span>
        <el-input name="mobile" type="text" v-model="registerForm.mobile" autoComplete="on" placeholder="请输入手机号"/>
      </el-form-item>

      <el-form-item prop="captcha" class="ww-border">
        <span class="svg-container svg-container_login">
          <!--<svg-icon icon-class="user"/>-->
            <i class="el-icon-message"></i>
        </span>
          <el-input name="captcha" type="text" v-model="registerForm.captcha" autoComplete="on" placeholder="请输入验证码"/>
          <timer class="timer" :start='start' @countDown='start=false' @click.native='sendCode'></timer>
      </el-form-item>

      <el-form-item prop="password" class="ww-border">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input name="password" :type="pwdType" @keyup.enter.native="handleRegister" v-model="registerForm.password"
                  autoComplete="on"
                  placeholder="请输入密码"></el-input>
        <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye"/></span>
      </el-form-item>
      <el-form-item >
        <el-button class="pull-left" style="display: inline-block;width: 100%;background: #66CC66;" type="primary" :loading="loading" @click.native.prevent="handleRegister">
          注册
        </el-button>
      </el-form-item>
      <el-form-item style="text-align: center;">
        <span style="text-decoration:underline;font-size: 18px;color: #4895FF;" @click="back">返回</span>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import timer from './timer.vue'
  import { isvalidUsername, validatMobile } from '@/utils/validate'
  import { getCode, registerUser } from '../../api/register'

  export default {
    components: {
      timer
    },
    data() {
      const validateUsername = (rule, value, callback) => {
        if (!isvalidUsername(value)) {
          callback(new Error('请输入正确的用户名'))
        } else {
          callback()
        }
      }
      const validatePass = (rule, value, callback) => {
        if (value.length < 5) {
          callback(new Error('密码不能小于5位'))
        } else {
          callback()
        }
      }
      const validateCode = (rule, value, callback) => {
        if (value.length !== 6) {
          callback(new Error('验证码为6位'))
        } else {
          callback()
        }
      }
      const isValidateMobile = (rule, value, callback) => {
        if (!validatMobile) {
          callback(new Error('请输入手机号'))
        } else {
          callback()
        }
      }
      return {
        start: false,
        registerForm: {
          authority: 'ROLE_VISITOR',
          name: '',
          mobile: '',
          password: '',
          captcha: ''
        },
        registerRules: {
          mobile: [{ required: true, trigger: 'blur', validator: isValidateMobile }],
          name: [{ required: true, trigger: 'blur' }],
          password: [{ required: true, trigger: 'blur', validator: validatePass }],
          captcha: [{ required: true, trigger: 'blur', validator: validateCode }]
        },
        loading: false,
        pwdType: 'text'
      }
    },
    methods: {
      back() {
        this.$router.back()
      },
      sendCode(value) {
        if (this.registerForm.mobile.length !== 11) {
          this.$message({
            message: '请输入手机号',
            type: 'error'
          })
          return
        }
        // 前面发送ajax请求成功之后调用this.start = true开始短信倒计时

        this.start = true
        getCode(this.registerForm.mobile).then(() => {
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleRegister() {
        const that = this
        this.$refs.registerForm.validate(valid => {
          if (valid) {
            this.loading = true
            registerUser(this.registerForm).then(() => {
              this.loading = false
              this.$alert('是否跳转到登录页面？', '注册成功', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                center: true
              }).then(() => {
                that.$router.push('/login')
              }).catch(() => {
                // do nothing
              })
            }).catch(() => {
              this.$message({
                message: '注册失败',
                type: 'error'
              })
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }

    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/mixin.scss";
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;
  .el-form-item{
    height: 50px;
    box-sizing: border-box;
  }
  .el-form-item__content{
    background: rgba(7,18,46,0.0);
  }
  .el-input{
    background: none;
  }
  .login-container {
    min-height: 750px;
    background: url("../../assets/images/register-bg.png") no-repeat center center ;
    background-size: 1280px 730px;
    margin-top: 100px;
    /*margin-top: 100px;*/
    // background-color: $bg;
    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px rgba(0,0,0,0) inset !important;
      background-color: rgba(250, 255, 189,0) !important;
      -webkit-text-fill-color: rgba(0,0,0,0) !important;
    }
    .ww-border{
      background: rgba(7,18,46,0.50);
      border: 2px solid #0099FF;
      border-radius: 2px;
    }
    input {
      background: rgba(0,0,0,0);
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
    }
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: #0099FF;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      margin: 0px auto 20px auto;
      text-align: center;
      font-weight: bold;
      font-size: 41.58px;
      color: #FFFFFF;
    }
    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 400px;
      padding: 13px 35px 15px 35px;
      margin: 140px auto;

    }
    .el-form-item {
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select:none;
    }
    .thirdparty-button{
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
  }
  .timer{
    top: 0px;
    right: 0px;
    bottom: 0px;
    height: 50px;
    position: absolute;
    box-sizing: border-box;
  }
</style>

